/*
 * Horspool.cpp
 *
 *  Created on: 27-08-2013
 *      Author: Krzychu
 */

#include "Horspool.h"

#include "../Structures/Config.h"

#include <stdio.h>
#include <string.h>

#define ASIZE 255

Horspool::Horspool()
{

}

Horspool::~Horspool()
{

}

std::string Horspool::getName()
{
	return "Horspool";
}

searchResult Horspool::execute(std::string text, std::string pattern)
{
	searchResult result;
	result.iterationsTaken = 0;
	int j = 0;
	char c;
	int textLength = text.length();
	int patternLength = pattern.length();

	const char* textChar = text.c_str();
	const char* patternChar = pattern.c_str();

	/* Preprocessing */
	int* bmBc = pre_bad_character_shift(pattern);

	/* Searching */
	while (j <= textLength - patternLength)
	{
		result.iterationsTaken++;
		c = text[j + patternLength - 1];
		if (pattern[patternLength - 1] == c && memcmp(patternChar, textChar + j, patternLength - 1) == 0)
		{
			result.positionsFound.push_back(j);
			if (Config::getInstance()->getFindOneOccurence())
			{
				return result;
			}
		}
		j += bmBc[c];
	}

	return result;
}

